package com.letou.ls.ui;

import java.util.ArrayList;
import java.util.List;

import org.apache.table.TableTitle;

import com.letou.ls.R;
import com.letou.ls.mode.AppConfigInfo;
import com.letou.ls.mode.LotteryInfo;
import com.letou.ls.uitl.AppConfigManager;
import com.letou.ls.uitl.Constant;
import com.letou.ls.uitl.Util;
import com.letou.ls.view.MLinearLayout;
import com.letou.ls.view.TableBadyLineView;
import com.letou.ls.view.TableFifFiveBottmBady;
import com.letou.ls.view.TableFifFiveTitleView;
import com.letou.ls.view.TableViewUtil;
import com.letou.ls.view.VerticalTextView;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

/**
 * 十一选五 横
 *
 * @author Administrator
 *
 */
public class FifFiveHarActivity extends BaseActivity {
    private LinearLayout mTopLayout;
    private LinearLayout mBottomLayout;
    private LinearLayout mContextLayout;
    private LinearLayout lLayout;
    private TableFifFiveTitleView mTableTopTitle;
    private TableBadyLineView mTableBadyView;
    private TableFifFiveBottmBady mTableBottmBady;
    
    private float textSize;
    private float itemHeight;
    private ArrayList<Float> widthList;
    private float density;
    private ArrayList<Integer> lineOne = new ArrayList<Integer>();
    private ArrayList<Integer> lineTwo = new ArrayList<Integer>();
    private ArrayList<Integer> lineThree = new ArrayList<Integer>();
    private ScrollView scrollview;
    private LinearLayout menuParent;
    private Button menuBtn;
    private TextView startLotto;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        lotteryCode = Constant.LOTTERY_CODE_04;
        screen_direction_type = 1;
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fif_five_hor);
        initFirstDateTime("9:04:59", "9:15:00");
        loadHorizontalLayout();
        
    }
    
    private void loadHorizontalLayout() {
        mTopLayout = (LinearLayout) findViewById(R.id.fif_five_hor_top_layout);
        mContextLayout = (LinearLayout) findViewById(R.id.fif_five_hor_context_layout);
        mBottomLayout = (LinearLayout) findViewById(R.id.fif_five_hor_bottom_layout);
        lLayout = (LinearLayout) findViewById(R.id.lLayout);
        scrollview = (ScrollView) findViewById(R.id.fif_five_hor_scrollview);
        menuParent = (LinearLayout) findViewById(R.id.majiang_menu_parent);
        menuBtn = (Button) findViewById(R.id.majiang_menu_bt);
        startLotto = (TextView) findViewById(R.id.startLotto);
        
        VerticalTextView vTop = (VerticalTextView) findViewById(R.id.vTextTop);
        String t1 = getResources().getString(R.string.tip_top);
        String t2 = getResources().getString(R.string.tip_bottom);
        String space = "      ";
        
        // VerticalTextView vBottom = (VerticalTextView) findViewById(R.id.vTextBottom);
        // vBottom.setText(getResources().getString(R.string.tip_bottom));
        
        DisplayMetrics metric = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metric);
        
        if (metric.widthPixels == 1920) vTop.setTextSize(40);
        vTop.setText(t1 + space + t2 + space + Util.getDataLine(this) + space);
        
        density = metric.density; // 屏幕密度（0.75 / 1.0 / 1.5）
        int viewWidth = 0;
        LayoutParams para = lLayout.getLayoutParams();
        if (metric.widthPixels == 1920) {
            para.width = 69 * 2;
            lLayout.setLayoutParams(para);
            viewWidth = metric.widthPixels - 69 * 2;
        } else if (metric.widthPixels == 1280) {
            para.width = 46 * 2;
            lLayout.setLayoutParams(para);
            viewWidth = metric.widthPixels - 46 * 2;
        }
        // = (int) (metric.widthPixels - 50 * density);
        
        TableTitle title = TableViewUtil.get115TableHor(this, viewWidth);
        
        
        // widthList = title.getmWidthList();
        initWidthList(viewWidth);
        
        
        mTableTopTitle = new TableFifFiveTitleView(this, widthList);
        LinearLayout.LayoutParams ll =
                new LinearLayout.LayoutParams(viewWidth, LayoutParams.WRAP_CONTENT);
        if (density < 1.8) {
            mTableTopTitle.addData(title, 1.8f);
        } else {
            mTableTopTitle.addData(title, density);
        }
        mTableTopTitle.setLayoutParams(ll);
        
        textSize = title.getDataTextSize();
        
        mTableBadyView = new TableBadyLineView(this);
        
        // itemHeight = textSize * 1.618f * density;
        itemHeight = widthList.get(3);
        // itemHeight = textSize * 1.26f * density;
        int size = 50;
        int tableHeight = (int) (itemHeight * size + size + 1);
        
        LinearLayout.LayoutParams badyParams =
                new LinearLayout.LayoutParams(viewWidth, tableHeight);
        mTableBadyView.setLayoutParams(badyParams);
        
        mTopLayout.addView(mTableTopTitle);
        mContextLayout.addView(mTableBadyView, badyParams);
        
        mTableBottmBady = new TableFifFiveBottmBady(this);
        mTableBottmBady.setLayoutParams(ll);
        
        mBottomLayout.addView(mTableBottmBady);
        
        scrollview.setOnTouchListener(new OnTouchListener() {
            
            @Override
            public boolean onTouch(View arg0, MotionEvent arg1) {
                
                return mGesture.onTouchEvent(arg1);
                // TODO Auto-generated method stub
            }
            
        });
        
        menuBtn.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                gotoMenuActivity();
            }
            
        });
    }
    
    private void initWidthList(int viewWidth) {
        AppConfigInfo info =
                AppConfigManager.getInstance(FifFiveHarActivity.this).getAppConfigInfo();
        // 必选 期号 开奖号码分布 和 前三分布 显示折线
        // 互斥 前三直 和 开奖号码互斥 2位 和 3位 显示折线 和 开奖号码分布
        // 可选 和值 跨度 大号个数 单号个数 推荐号 遗漏值 出现次数
        Log.e("cc", "initWidthList:" + info.toString());
        int count = 54;
        float lineCount = 53;
        if (!info.getKuadu()) {// 跨度
            count -= 2;
            lineCount -= 2;
        }
        if (!info.getHezhi()) {// 和值
            count -= 2;
            lineCount -= 2;
        }

        if (!info.getKuadu() && !info.getHezhi()) {
            lineCount -= 16;
        }

        widthList = new ArrayList<Float>();
        float item = (((viewWidth - lineCount) / (count)));
        Log.e("cc", "initWidthList:count:" + count + ";viewWidth:" + viewWidth + ";item:" + item);
        int i = 0;
        for (i = 0; i < 54; i++) {
            switch (i) {
                case 52:
                case 17:
                    if (info.getKuadu()) {// 跨度
                        widthList.add(item * 1);
                    } else {
                        widthList.add(item * 0);
                    }
                    
                    break;
                case 53:
                case 18:
                    if (info.getHezhi()) {// 和值
                        widthList.add(item * 1);
                    } else {
                        widthList.add(item * 0);
                    }
                    break;
                
                default:
                    widthList.add(item);
                    break;
            }
        }
        
        
        // widthList = new ArrayList<Float>();
        // int i = 0;
        // if (metric.widthPixels % 30 == 0) {// 1920
        // for (i = 0; i < 54; i++) {
        // widthList.add((float) (viewWidth / 54 - 1));
        // }
        // } else if (metric.widthPixels % 32 == 0) {// 1280
        // for (i = 0; i < 54; i++) {
        // widthList.add((float) (viewWidth / 54 - 1));
        // }
        // }
    }
    
    @Override
    protected void onStart() {
        // TODO Auto-generated method stub
        super.onStart();
    }
    
    @Override
    protected void updateView(List<LotteryInfo> lotteryInfo, String miss, String sum) {
        if (lotteryInfo.isEmpty()) {
            return;
        }
        lineOne.clear();
        lineTwo.clear();
        lineThree.clear();
        ArrayList<ArrayList<ArrayList<String>>> data =
                new ArrayList<ArrayList<ArrayList<String>>>();
        int size = lotteryInfo.size();
        
        for (int index = 0; index < size; index++) {
            
            ArrayList<ArrayList<String>> itemList = new ArrayList<ArrayList<String>>();
            LotteryInfo info = lotteryInfo.get(index);
            String value = info.getLotteryNumbers();
            String[] array = value.split(",");
            int arraySize = array.length;
            int[] all = new int[arraySize];
            int count = 0;
            for (int j = 0; j < arraySize; j++) {
                all[j] = Integer.valueOf(array[j]);
                count += all[j];
            }
            int[] aa = {all[0], all[1], all[2]};
            
            String issue = info.getIssue();
            issue = issue.substring(issue.length() - 2, issue.length());
            if (isFirstData(info.getDateline())) {
                
                TableViewUtil.addCol(issue, 4, itemList);// 1
            } else {
                TableViewUtil.addCol(issue, 0, itemList);// 1
            }
            
            TableViewUtil.addCol(array[0], 2, itemList);// 2
            TableViewUtil.addCol(array[1], 2, itemList);// 3
            TableViewUtil.addCol(array[2], 2, itemList);// 4
            TableViewUtil.addCol(array[3], 3, itemList);// 5
            TableViewUtil.addCol(array[4], 3, itemList);// 6
            
            TableViewUtil.getDataArray(aa, all, itemList, 11);
            TableViewUtil.addCol("" + (all[array.length - 1] - all[0]), 3, itemList);// 25
            TableViewUtil.addCol("" + count, 3, itemList);// 26
            
            int numOne = Integer.valueOf(array[0]);
            int numTwo = Integer.valueOf(array[1]);
            int numThree = Integer.valueOf(array[2]);
            
            TableViewUtil.getDataArray(numOne, 1, itemList, 11);
            lineOne.add(numOne);
            TableViewUtil.getDataArray(numTwo, 2, itemList, 11);
            lineTwo.add(numTwo);
            TableViewUtil.getDataArray(numThree, 1, itemList, 11);
            lineThree.add(numThree);
            
            int max = numOne;
            int min = numOne;
            if (numOne > numTwo) {
                max = numOne;
                min = numTwo;
            } else {
                min = numOne;
                max = numTwo;
            }
            if (max < numThree) {
                max = numThree;
            }
            if (min > numThree) {
                min = numThree;
            }
            
            TableViewUtil.addCol("" + (max - min), 3, itemList);// 25
            TableViewUtil.addCol("" + (numOne + numTwo + numThree), 3, itemList);// 26
            
            data.add(itemList);
            
        }
        mTableBadyView.addLine(lineOne, lineTwo, lineThree, TableViewUtil.redLine11_5har);
        mTableBadyView.startLine(19, 30, 41);
        mTableBadyView.updateData(data, widthList, (textSize + 2) * density, itemHeight);
        
        ArrayList<ArrayList<String>> bottomdata = new ArrayList<ArrayList<String>>();
        ArrayList<String> bottomitem = new ArrayList<String>();
        bottomitem.add("当前遗漏值");
        String[] missArray = miss.split(",");
        size = 44;
        for (int index = 0; index < size; index++) {
            bottomitem.add(missArray[index]);
        }
        bottomitem.add("");
        bottomitem.add("");
        bottomdata.add(bottomitem);
        bottomitem = new ArrayList<String>();
        bottomitem.add("出现总次数");
        String[] sumArray = sum.split(",");
        size = 44;
        for (int index = 0; index < size; index++) {
            bottomitem.add(sumArray[index]);
        }
        bottomitem.add("");
        bottomitem.add("");
        bottomdata.add(bottomitem);
        ArrayList<Float> list = TableViewUtil.getBottomWidth(widthList, 6, 17, 18);
        mTableBottmBady.addData(bottomdata, TableViewUtil.redL11_5har_bo, list, textSize,
                R.drawable.icon_115_har, 12);
        scrollview.post(new Runnable() {
            
            @Override
            public void run() {
                // TODO Auto-generated method stub
                scrollview.fullScroll(ScrollView.FOCUS_DOWN);
            }
        });
        
        final MLinearLayout layout = (MLinearLayout) mBottomLayout;
        layout.reMeasure();
        
        LayoutParams lp = startLotto.getLayoutParams();
        lp.height = layout.getMeasuredHeight() / 2 - 1;
        startLotto.setLayoutParams(lp);
        
        lp = menuParent.getLayoutParams();
        lp.height = layout.getMeasuredHeight() / 2 - 2;
        menuParent.setLayoutParams(lp);
        
        System.out.println("bottom height : " + layout.getMeasuredHeight());
    }
    
    @Override
    public void updateTimeCount(String time) {
        if(!TextUtils.isEmpty(time)){
        	 startLotto.setText(time);
        }else{
        	 startLotto.setText(mCountdownTime);	
        }
       
    }
}
